package com.visiontech.volunteer_center.controller;

import com.visiontech.volunteer_center.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.channels.SeekableByteChannel;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserService userService;

    @GetMapping("/login")
    public String loginPage(){
        return "login";
    }

    @GetMapping("/register")
    public String registerPage(){
        return "register";
    }

    @PostMapping("/verify")
    public void verifyUser(String account, Integer roleId, String password, HttpServletResponse response, HttpSession session) throws IOException {
        String message = userService.verifyUser(account, roleId, password);
        if (message == null) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;setchar=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<script> var r=confirm('用户不存在，是否进行注册？');if(r==true){window.location.href='/user/register'}else{window.location.href='/user/login'};</script>");
            out.close();
        } else if(message.equals("error")){
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;setchar=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<script> alert('账号或密码错误！');window.location.href='/user/login';</script>");
            out.close();
        } else if(message.equals("success")){
            session.setAttribute("account", account);
            session.setAttribute("roleId", roleId);
//            response.setCharacterEncoding("utf-8");
//            response.setContentType("text/html;setchar=utf-8");
//            PrintWriter out = response.getWriter();
//            out.println("<script> alert('登陆成功！');window.location.href='/content/index_user';</script>");
//            out.close();
            response.sendRedirect("/content/index_user");
        }
    }

    @PostMapping("/insert")
    public void insertUser(@RequestParam("userName") String userName, @RequestParam("account") String account,
                           @RequestParam("roleId") Integer roleId, @RequestParam("password") String password,
                           @RequestParam("email") String email, @RequestParam("phone") String phone,
                           HttpServletResponse response) throws IOException{
        if(userService.insertUser(userName, account, roleId ,password, email, phone) == 1){
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;setchar=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<script> alert('注册成功！');window.location.href='/user/login';</script>");
            out.close();
        } else {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;setchar=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<script> alert('用户已存在！');window.location.href='/user/register';</script>");
            out.close();
        }
    }

    @GetMapping("/exit")
    public String exitUser(HttpSession session){
        session.removeAttribute("account");
        session.removeAttribute("roleId");
        return "redirect:/user/login";
    }

}
